// pages/myAddr/myAddr.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        defCheckValue: '',
        currentCheckValue: '',
        addressList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getAddressListData();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    getAddressListData: function () {
        var that = this;
        // request
        var params = {
            userID: app.globalData.userID
        }
        app.network.requestPost(app.route.ROUTE_GET_ADDRESS_LIST, params, '正在获取数据', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                var _addressList = res.context.addressList;
                if (_addressList.length) {
                    for (var item of _addressList) {
                        if (item.isDefaultAddress == 1) {
                            item['checked'] = true;
                            item['currentCheckValue'] = true;
                            that.setData({
                                defCheckValue: item.addressID,
                                addressList: _addressList
                            })
                        } else {
                            that.setData({
                                defCheckValue: '',
                                addressList: _addressList
                            })
                        }
                    }
                } else {
                    that.setData({
                        defCheckValue: '',
                        addressList: []
                    })
                }
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    radioChange: function (e) {
        var defCheckValue = e.detail.value;
        var that = this;
        var params = {
            userID: app.globalData.userID,
            addressID: defCheckValue
        }
        app.network.requestPost(app.route.ROUTE_SET_DEFAULT_ADRESS, params, '正在设置数据', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                var _addressList = that.data.addressList;
                wx.showToast({
                    title: '设置成功'
                })
                for (var item of _addressList) {
                    item.addressID == defCheckValue ? item['checked'] = true : item['checked'] = '';
                    var _defCheckValue = item.addressID == defCheckValue ? item.addressID : that.data.defCheckValue;
                    that.setData({
                        defCheckValue: _defCheckValue,
                        addressList: _addressList
                    })
                }
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    usedAddrTap: function(e) {
        var _addressList = this.data.addressList;
        var _addressID = e.currentTarget.dataset.addressid;
        for (var item of _addressList) {
            item.currentCheckValue = item.addressID == _addressID ? true : false;
        }
        this.setData({
            addressList: _addressList
        })
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.changeAddr(_addressID); //调用上个页面函数，传递参数无感知刷新数据

        var that = this;
        var params = {
            userID: app.globalData.userID,
            addressID: _addressID
        }
        app.network.requestPost(app.route.ROUTE_SET_DEFAULT_ADRESS, params, '', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    delAddrTap: function(e) {
        var _addressID = e.currentTarget.dataset.addressid;
        var that = this;
        var params = {
            userID: app.globalData.userID,
            addressID: _addressID
        }
        app.network.requestPost(app.route.ROUTE_DEL_ADDRESS, params, '正在删除', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                wx.showToast({
                    title: '删除成功'
                })
                that.getAddressListData();
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    addAddrTap: function (e) {
        wx.navigateTo({
            url: '../addAddr/addAddr'
        })
    },
    editAddrTap: function(e) {
        var _addressID = e.currentTarget.dataset.addressid;
        wx.navigateTo({
            url: '../addAddr/addAddr?addressID=' + _addressID
        })
    }
})